/**
 * Created by Administrator on 2018/7/20.
 */
var nowContractCode;
//初始化 日期控件

$$scope.moneyChange = function(money,b,c){
    $$scope.contractInfo[c+"Date"] = "";
    initDatePicker(b,c);
}

function initDate(a,b,c){

    var picker34 = $(b).datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    }).on('dp.change', function (e) {
        var result = new moment(e.date).format('YYYY-MM-DD');
        if (result && !angular.isUndefined(result)) {
            $$scope.contractInfo[c+"Date"] = result;
            $$scope.$apply();
        }
    })
    $(a).focus();
}

function initDatePicker(b,c){
    var picker34 = $(b).datetimepicker({
        format: 'YYYY-MM-DD',
        locale: moment.locale('zh-cn')
    }).on('dp.change', function (e) {
        var result = new moment(e.date).format('YYYY-MM-DD');
        if (result && !angular.isUndefined(result)) {
            $$scope.contractInfo[c+"Date"] = result;
            $$scope.$apply();
        }
    })
}

//定时查询 合同最新信息
$$scope.getState = function(a){
    if (a == "1") {
        return "运行中"
    } else if (a == "2") {
        return "已关闭"
    } else if (a == "3") {
        return "已完结"
    } else if (a == "4") {
        return "已暂停"
    }
}
//查询项目基本信息
$CertusFormAjax.ajax("contractProjInfo", {
    projId: proId
}, function (res) {
    $$scope.proj = res.data;
})




$CertusFormAjax.ajax("contractProgresss", {
    projId: proId
}, function (res) {
    if (res.data) {
        $$scope.allContractInfo = res.data;
        $$scope.timer1 = $$timeout(function () {
            if($$scope.allContractInfo && $$scope.allContractInfo.length > 0){
                if ($("#stepBar-2-0").html()) {
                    extracted();
                } else {
                    $$scope.timer2 = $$timeout(function () {
                        if ($("#stepBar-2-0").html()) {
                            extracted();
                        }else{
                            $$scope.timer3 = $$timeout(function () {
                                if ($("#stepBar-2-0").html()) {
                                    extracted();
                                }else{
                                    $$scope.timer4 = $$timeout(function () {
                                        if ($("#stepBar-2-0").html()) {
                                            extracted();
                                        }else{
                                            layer.msg("重载 重启页面");
                                            window.location.reload()
                                        }
                                    }, 50)
                                }
                            }, 50)
                        }
                    }, 50)
                }
            }

        }, 50);

        // stepBar.init("stepBar-2-0", {
        //     step: 6, //从 后台 取
        //     change: false,
        //     animation: true,
        //     callback:function(){
        //
        //     }
        // });
    }
})

var pactColumns = [{
    id: 'fileCategories',
    title: '文件类目',
    width: 120,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'fileClass',
    title: '文件类别',
    width: 120,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'fileOriginName',
    title: '文件名',
    // width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'fileDescriptor',
    title: '文件说明',
    type: 'string',
    columnClass: 'text-left',
    hideType: 'lg|md|sm|xs'
}, {
    id: 'version',
    title: '文件版本',
    width: 80,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'createUserId',
    title: '上传人员',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'createTime',
    title: '上传时间',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var date = new Date(record.createTime);
        var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd hh:mm");
        return content;
    }
}, {
    id: 'operation',
    title: '操作',
    type: 'string',
    width: 150,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        if (record.id && record.id != "yes") {
            content += '<button class="btn btn-xs btn-success" onclick="alterProjFileContract(\'' + record.id + '\',this);"><i class="icon-cloud-upload"></i>  更新</button>';
        }
        content += '  ';
        content += '<button class="btn btn-xs btn-info" onclick="down(\'' + record.id + '\',this);"><i class="icon-cloud-download"></i>  下载</button>';
        return content;
    }
}];
//加载 stepBar 以及 下面的datagrid
function extracted() {
// $$interval.cancel($$scope.timer1);

    var data = $$scope.allContractInfo;
    data.forEach(function (obj, index) {
        // stepBar.init("stepBar-2" + "-" + index, {
        //     step: 6, //从 后台 取
        //     change: false,
        //     animation: true
        // });

        stepBar.init("stepBar-2" + "-" + index, {
            step: obj.phaseNum, //从 后台 取
            change: false,
            animation: true
        });
    })
    var met = {
        'XMHTXXC': "alterProjFileContract(\'' + record.id + '\',this);",
        'XMHTXXR': "down(\'' + record.id + '\',this);"
    }
    content = $CertusFormAjax.initOperateAutho($$scope.leftMenu, ['XMHTXXC%upload', 'XMHTXXR%download'], met, pactColumns);
    angular.forEach(data, function (obj, index) {
        $("#"+'pactContainer-' + index).html("");
        $CertusFormAjax.ajax("disContractFileDatagrid", {
            contractCode: obj.contractInfo.contractCode,
            projId: proId
        }, function (res) {
            var tenderOption = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                // loadURL:'url',
                exportFileName: '投标文件列表',
                datas: res.data,
                columns: pactColumns,
                gridContainer: 'pactContainer-' + index,
                toolbarContainer: 'pactToolBarContainer-' + index,
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            var tenderGrid = $.fn.dlshouwen.grid.init(tenderOption);
            tenderGrid.load();
        })
    })
}

//按钮点击 跳转方法

//下载页面 弹框


//合同 文件 列表 可新增


//合同文件 覆盖修改
function alterProjFileContract(a, b) {
    nowContractCode = $(b).parent().parent().parent().parent().parent().parent().find(".nowContractCode").val();
    //console.log("进入跳转的第一个页面");
    $("#rightContainer").empty();
    $$http({
        // url: "proinfo/edit/edit-compact.html",
        url: "proinfo/edit/edit-compact-file-cover.html",
        method: 'GET',
        data: {},
        dataType: "application/json"
    }).then(function (a) {
        $$scope.htmlxx = a.data
    }, function (b) {
        //console.log("加载 合同 新增 文件 页面 失败");
    })
}

//修改 合同信息 提交页面
$$scope.alterInfoSubmit = function () {
    if (certusValidateNotnull()) {
        $$scope.contractInfo.templateCode = "HTXXSP";
        //判断 contractNo 合同编号 是否已经存在
        $CertusFormAjax.ajax("confirmContractNo", {
            contractNo: $$scope.contractInfo.contractNo,
            projId: proId
        }, function (res) {
            if (res.data == "no") {
                //检查 相加 之和 是否是 100
                // var number1 = parseInt($$scope.contractInfo.initialPayment);
                // var number2 = parseInt($$scope.contractInfo.preliminaryInspection);
                // var number3 = parseInt($$scope.contractInfo.finalAcceptance);
                // var number4 = parseInt($$scope.contractInfo.finalPayment);

                // if((number1 + number2 + number3 + number4) == 100){
                //     //可以 提交
                //     $CertusJsonAjax.ajax("saveContractInfo", $$scope.contractInfo, function (res) {
                //         if (res.data) {
                //             if (res.data == "success") {
                //                 findBacklogPersonHead($CertusFormAjax, "HTXXSP");
                //                 disProjContactInfo();
                //             }
                //         }
                //     })
                // }else{
                //     layer.msg("付款条款 相加之和未等于100");
                // }

                $CertusJsonAjax.ajax("saveContractInfo", $$scope.contractInfo, function (res) {
                    if (res.data) {
                        if (res.data == "success") {
                            findBacklogPersonHead($CertusFormAjax, "HTXXSP");
                            disProjContactInfo();
                        }
                    }
                })

            } else {
                //合同编号 已经 存在
                layer.msg("合同编号已经存在，请重新填写。");
            }

        })

    }
}


$$scope.coverContractFileSubmmit = function () {
    $$scope.contractFile = {
        contractCode: nowContractCode,
        projId: proId
    };

    if(certusValidateNotnullPolyfill()){
        $$scope.businessQuoteApprove = fileUpload($$scope, function (res) {
            $$scope.contractFile.filePathId = res.data;
            $$scope.contractFile.opinionMsg = "";
            $$scope.contractFile.templateCode = "HTWJSP";
            $CertusJsonAjax.ajax("updateContractFilePath", $$scope.contractFile, function (res) {
                if (res.data == "success") {
                    findBacklogPersonHead($CertusFormAjax, "HTWJSP");
                    disProjContactInfo();
                }
            })
        }, function () {
            layer.msg("提交文件失败");
        })
    }
}


//合同基本信息 修改 页面 打开
function alterContractInfo() {
    //console.log("进入跳转的第一个页面");
    $("#rightContainer").empty();
    $$http({
        // url: "proinfo/edit/edit-compact.html",
        url: "proinfo/edit/edit-compact-info.html",
        method: 'GET',
        data: {},
        dataType: "application/json"
    }).then(function (a) {
        $$scope.contractInfo = {
            projId: proId
        };
        $$scope.htmlxx = a.data
    }, function (b) {
        //console.log("加载 合同 新增 文件 页面 失败");
    })
}

$$scope.getProjLv = function (projLv) {
    if (projLv == "1") {
        return "低";
    } else if (projLv == "2") {
        return "中";
    } else if (projLv == "3") {
        return "高"
    }
}

//文件列表下载
function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}

//下载弹框打开
function down(a, b) {
    ;
    $CertusJsonAjax.ajax("contractDownFileList", {
        contractCode: $(b).parent().parent().parent().parent().parent().parent().find(".nowContractCode").val(),
        projId: proId
    }, function (data) {

        if(data.data && data.data.length > 0){
            $$scope.modalContent = data.data;
            $$scope.ok = function () {
                $modalInstance.close(); // 点击确认后关闭弹窗
            };
            $$scope.cancel = function () {
                $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
            }
            var modalContent = $$scope.modalContent;

            $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
            });

        }else{
            layer.msg("暂时无文件，可供下载");
        }
    })
}

// //新增合同文件
// $$scope.addContractFileSubmmit = function () {
//     $$scope.businessQuoteApprove = fileUpload($$scope, function (res) {
//         $$scope.contractFile.uploadFilePath = res.data;
//         $$scope.contractFile.templateCode = '';
//         $$scope.contractFile.opinionMsg = "";
//         $CertusJsonAjax.ajax("startBusinessQuote", $$scope.business, function (res) {
//             if (res.data.succed == "success") {
//                 window.history.back();
//             }
//         })
//     }, function () {
//         $$scope.contractFile.uploadFilePath = "";
//         $$scope.contractFile.opinionMsg = "";
//         $CertusJsonAjax.ajax("subBusinessQuote", $$scope.business, function (res) {
//             if (res.data.succed == "success") {
//                 window.history.back();
//             }
//         })
//     })
// }
//修改合同文件 覆盖

// //合同文件 新增
// function increaseProjContract() {
//     console.log("进入跳转的新增页面");
//     $("#rightContainer").empty();
//     $$http({
//         // url: "proinfo/edit/edit-compact.html",
//         url: "proinfo/edit/edit-compact-file.html",
//         method: 'GET',
//         data: {},
//         dataType: "application/json"
//     }).then(function (a) {
//         $$scope.htmlxx = a.data
//     }, function (b) {
//         console.log("加载 合同 新增 文件 页面 失败");
//     })
// }